package com.sky.Task;

import com.sky.context.BaseContext;
import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-23 9:49
 */
@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 处理支付超时订单
     */
    @Scheduled(cron = " 0 */1 * * * ?")
    public void  execute(){
        LocalDateTime now = LocalDateTime.now();
        log.info("每隔1分钟执行:{}", now);

        LocalDateTime localDateTime = now.plusMinutes(-15);
        //查询订单在待支付状态的数据
        List<Orders> orders= ordersMapper.findBystatus(Orders.PENDING_PAYMENT,localDateTime);
        if (orders==null || orders.size()==0){
            log.info("不存在超时未支付订单");
            return;
        }
            ordersMapper.orderupdate(orders,Orders.CANCELLED);
        log.info("处理未支付订单处理了:{}笔订单",orders.size());
    }

    /**
     * 处理派送中的订单
     */
    @Scheduled(cron = " 0 0 1 * * ?")
    public void  processDeliveryOrder(){
        LocalDateTime now = LocalDateTime.now();
        log.info("每隔1小时执行:{}", now);

        LocalDateTime localDateTime = now.plusMinutes(-60);
        //查询订单在待支付状态的数据
        List<Orders> orders= ordersMapper.findBystatus(Orders.DELIVERY_IN_PROGRESS,localDateTime);
        if (orders==null || orders.size()==0){
            log.info("不存在派送中订单");
            return;
        }
        ordersMapper.orderupdate(orders,Orders.COMPLETED);
        log.info("处理派送中订单处理了:{}笔订单",orders.size());
    }

}
